package org.graduates.webapp.actions.person;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.Constants;
import org.projects.graduates.domain.User;
import org.projects.graduates.utils.EncryptionTool;

@Action(results = { @Result(name = "success", type = "json") })
public class UpdatePasswordAction extends BaseAction {

	private static final long serialVersionUID = -6263941048979296659L;

	private long uid = 0L;

	private String error;

	private String oldPsw;

	private String newPsw;

	public String execute() {

		if (0L == uid) {
			error = "不存在用户";
			return SUCCESS;
		}
		
		User user = User.get(User.class, uid);
		
		if (null == user) {
			error = "不存在用户";
			return SUCCESS;
		}
		
		if(user.getPassword().equals(EncryptionTool.convert(oldPsw, Constants.SECURITY_TYPE))){
			user.setPassword(EncryptionTool.convert(newPsw,Constants.SECURITY_TYPE));
			this.gradApplication.save(user);
		}else{
			error = "密码错误";
			return SUCCESS;
		}

		return SUCCESS;
	}

	public String getError() {
		return error;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public void setOldPsw(String oldPsw) {
		this.oldPsw = oldPsw;
	}

	public void setNewPsw(String newPsw) {
		this.newPsw = newPsw;
	}

}
